package com.keste.custom.ui.bean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import javax.el.ELContext;

import javax.el.ExpressionFactory;

import javax.el.ValueExpression;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import javax.faces.event.ValueChangeEvent;

import oracle.adf.share.ADFContext;
import oracle.adf.view.rich.component.fragment.UIXDeclarativeComponent;

public class QuestionComponentBean {
    
    private UIXDeclarativeComponent qComponent;
    private HashMap questionMap;
    private HashMap qAnswerOptions;
    private String questionType;
    private String displayType;
    private Integer questionId;
    private String questionText;
    
    private List selectedAnswers;
    
    public QuestionComponentBean() {
        qComponent = (UIXDeclarativeComponent)getValueObject("#{component}",UIXDeclarativeComponent .class);
        questionMap = (HashMap)qComponent.getAttributes().get("Question");
        questionId = (Integer)(questionMap.keySet().toArray())[0];
        questionText = (String)questionMap.get(questionId);
        qAnswerOptions = (HashMap)qComponent.getAttributes().get("Options");
        questionType = (String)qComponent.getAttributes().get("QuestionType");
        displayType = (String)qComponent.getAttributes().get("DisplayType");
        
        
        selectedAnswers = new ArrayList();
    }
    
    private Object getValueObject(String expr, Class returnType){
         FacesContext fc = FacesContext.getCurrentInstance();
        ELContext elctx = fc.getELContext();
        ExpressionFactory elFactory =
        fc.getApplication().getExpressionFactory();
        ValueExpression valueExpr =
        elFactory.createValueExpression(elctx,expr,returnType);
        return valueExpr.getValue(elctx);

    }

    public void setQAnswerOptions(HashMap qAnswerOptions) {
        this.qAnswerOptions = qAnswerOptions;
    }

    public Map getQAnswerOptions() {
        return qAnswerOptions;
    }
    
    public Object[] getQuestionOptions() {  
        return qAnswerOptions.keySet().toArray();  
    }

    public void setQComponent(UIXDeclarativeComponent qComponent) {
        this.qComponent = qComponent;
    }

    public UIXDeclarativeComponent getQComponent() {
        return qComponent;
    }

    public void onSelectAnswer(ValueChangeEvent valueChangeEvent) {

        Boolean value = (Boolean)valueChangeEvent.getNewValue();
        Integer answerId = Integer.parseInt((String)valueChangeEvent.getComponent().getAttributes().get("selectedAnswerId"));
        
        if(questionType.equals("Q_M_Choice"))
        {
            if(selectedAnswers.contains(answerId) && !value)
            {
                selectedAnswers.remove(answerId);
            }
            else if(!selectedAnswers.contains(answerId) && value)
            {
                selectedAnswers.add(answerId);
            }
        }
        else if(questionType.equals("Q_S_Choice") || questionType.equals("Q_B_Choice"))
        {
            selectedAnswers.clear();
            selectedAnswers.add(answerId);
        }
        
        ADFContext.getCurrent().getViewScope().put("SelectedAnswers", selectedAnswers);
    }
    
    public boolean getIsSingleChoice()
    {
        if(questionType.equals("Q_S_Choice") || questionType.equals("Q_B_Choice"))
            return true;
        else
            return false;
    }

    public void setQuestionText(String questionText) {
        this.questionText = questionText;
    }

    public String getQuestionText() {
        return questionText;
    }
}
